﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using Newtonsoft.Json.Linq;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Service.DataMaintenance;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;

namespace PhysicalExamination5.WebApi.Controllers.DataMaintenance
{
    /// <summary>
    /// 科室维护
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class KeShiWeiHuController : Controller
    {
        private readonly IKeShiWeiHu _IKeShiWeiHu;
        private static readonly ILog log = LogManager.GetLogger(typeof(KeShiWeiHuController));
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICustomJWTService _ICustomJWTService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IKeShiWeiHu"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public KeShiWeiHuController(IKeShiWeiHu IKeShiWeiHu, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _IKeShiWeiHu = IKeShiWeiHu;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;

            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }
        /// <summary>
        /// 科室维护添加
        /// </summary>
        /// <param name="KeShiAddClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostKeShiAdd(KeShiAddClass KeShiAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQuQianZhui=  token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_IKeShiWeiHu.PostKeShiAdd(KeShiAddClass,YuanQuQianZhui));
        }
        /// <summary>
        /// 科室维护修改
        /// </summary>
        /// <param name="KeShiUpdateClass"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutKeShiUpdate(KeShiUpdateClass KeShiUpdateClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.PutKeShiUpdate(KeShiUpdateClass));
        }
        /// <summary>
        /// 科室维护重大疾病模板添加
        /// </summary>
        /// <param name="KeShiZhongDaJiBingAddClass"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult PostKeShiZhongDaJiBingAdd(KeShiZhongDaJiBingAddClass KeShiZhongDaJiBingAddClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.PostKeShiZhongDaJiBingAdd(KeShiZhongDaJiBingAddClass));
        }
        /// <summary>
        /// 科室维护重大疾病模板删除
        /// </summary>
        /// <param name="KeShiZhongDaJiBingDelClass"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteKeShiZhongDaJiBingDel(KeShiZhongDaJiBingDelClass KeShiZhongDaJiBingDelClass)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.DeleteKeShiZhongDaJiBingDel(KeShiZhongDaJiBingDelClass));
        }
        /// <summary>
        /// 科室维护重大疾病模板查询
        /// </summary>
        /// <param name="strKeShiID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetKeShiZhongDaJiBingSel(string strKeShiID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.GetKeShiZhongDaJiBingSel(strKeShiID));
        }
        /// <summary>
        /// 根据院区名称查询科室
        /// </summary>
        /// <param name="strYuanQuMingCheng"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetKeShiSel()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var YuanQu = token.Claims.FirstOrDefault(t => t.Type == "YuanQu")?.Value;
            return Ok(_IKeShiWeiHu.GetKeShiSel(YuanQu));
        }
        /// <summary>
        /// 科室维护删除
        /// </summary>
        /// <param name="strKeShiID"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteKeShiDel(string strKeShiID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.DeleteKeShiDel(strKeShiID));

        }
        /// <summary>
        /// 查询院区
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetYuanQuMingCheng()
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.GetYuanQuMingCheng());
        }
        /// <summary>
        /// 调整顺序
        /// </summary>
        /// <param name="iQianShunXu"></param>
        /// <param name="iHouShunXu"></param>
        /// <param name="strKeShiId"></param>
        /// <param name="strYuanQu"></param>
        /// <returns></returns>
        [HttpPut]
        [Authorize]
        [Route("[action]")]
        public IActionResult PutTiaoZhengShunXu(int iQianShunXu, int iHouShunXu, string strKeShiId)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_IKeShiWeiHu.PutTiaoZhengShunXu(iQianShunXu, iHouShunXu, strKeShiId));
        }

    }

}
